<template>
  <el-row style="width: 100%" type="flex">
    <el-col>
      {{ treeNode.name }}
    </el-col>
    <el-col :span="6">
      <el-row justify="space-between" type="flex">
        <el-col>
          <span style="margin-right: 20px">{{ treeNode.manager }}</span>
        </el-col>
        <el-col>
          <el-dropdown @command="commandFN">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加部门</el-dropdown-item>
                <!--如果是头部card组件内部调用会传递isRoot属性为true-->
                <!--编辑和删除只在树组件内部调用(isRoot: false)的时候展示出来-->
                <el-dropdown-item v-if="!isRoot" command="edit" divided>编辑部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del" divided>删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>

import { delDepartments } from '@/api/departments'

export default {
  name: 'TreeNode',
  props: {
    treeNode: {
      type: Object

    },
    isRoot: {
      type: Boolean
    }
  },
  methods: {
    async commandFN(type) {
      console.log(type)

      if (type === 'del') {
        await this.$confirm('确认删除吗?')
        await delDepartments(this.treeNode.id)
        this.$message.success('删除成功')
        this.$emit('getdepart')
      } else if (type === 'add') {
        this.$emit('addDepart', this.treeNode)
      } else if (type === 'edit') {
        this.$emit('editDept', this.treeNode)
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.el-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  width: 100%;
}
</style>
